package lendingbooks.android;

import java.util.HashMap;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import lendingbooks.GlobalState;
import lendingbooks.android.book.Book;
import lendingbooks.utils.ServiceCallException;
import lendingbooks.utils.ServiceManager;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class MyLibraryActivity extends Activity {
	GlobalState gs;
	private ServiceManager sm = new ServiceManager();
	private TextView menssage;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_library);		
		gs = (GlobalState)getApplicationContext();
		menssage = (TextView) findViewById(R.id.message);	
		_LoadMyLibrary();
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("idUser",gs.getUserId().toString());
		String result = "";
		try {
			result = sm.call(ServiceManager.SERVICE.MY_LIBRARY, params);
		} catch (ServiceCallException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		menssage.setText(result);
		Gson gson = new Gson();
		@SuppressWarnings("unchecked")
		List<Book> books = (List<Book>) gson.fromJson(result, new TypeToken<List<Book>>(){}.getType());
		menssage.setText(books.size() +"_"+ result );
		TableLayout tableLayout = (TableLayout) findViewById(R.id.myLibrary);
		TableRow tr = null;
		for(Book book: books){
			tr = new TableRow(this);
			TextView labelIsbn = new TextView(this);
			TextView labelTitle = new TextView(this);
			labelIsbn.setText(book.isbn);
			labelTitle.setText(book.title);
			tr.addView(labelIsbn);
			tr.addView(labelTitle);
			tableLayout.addView(tr);
		}
		
	}
	
	private void _LoadMyLibrary(){
		
		
	}
	
}
